
<!DOCTYPE html>
<html lang="zh-CN" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>后处理技术（中） - 浪子之心</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Blog,"> 
    <meta name="description" content="MrChen Bolg,模糊处理本文章参考毛星云大佬的文章学习，十分推荐看大佬的原文
降采样是常见的优化性能的方法减少中间贴图的尺寸大小
产生的中间贴图一定要记得释放内存，否则造成内存泄漏
在for循环的中间贴图也要记得释,"> 
    <meta name="author" content="Jack Chen"> 
    <link rel="alternative" href="atom.xml" title="浪子之心" type="application/atom+xml"> 
    <link rel="icon" href="/blog/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/blog/css/diaspora.css">

<meta name="generator" content="Hexo 5.4.1"><link rel="stylesheet" href="/blog/css/prism.css" type="text/css"></head>

<body class="loading">
    <span id="config-title" style="display:none">浪子之心</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://mrchenlearnspace.github.io/blog"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">后处理技术（中）</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">后处理技术（中）</h1>
        <div class="stuff">
            <span>二月 22, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/blog/tags/Shader/" rel="tag">Shader</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="模糊处理"><a href="#模糊处理" class="headerlink" title="模糊处理"></a>模糊处理</h1><p>本文章参考<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/125744132">毛星云大佬的文章</a>学习，十分推荐看大佬的原文<img src="https://pic3.zhimg.com/80/v2-c2a9906331826e66f66ffda71192411a_720w.jpg"></p>
<p><strong>降采样是常见的优化性能的方法减少中间贴图的尺寸大小</strong></p>
<p><strong>产生的中间贴图一定要记得释放内存，否则造成内存泄漏</strong></p>
<p><strong>在for循环的中间贴图也要记得释放内存</strong></p>
<h1 id="课程的4种常用的模糊算法"><a href="#课程的4种常用的模糊算法" class="headerlink" title="课程的4种常用的模糊算法"></a>课程的4种常用的模糊算法</h1><h2 id="盒状模糊或均值模糊（Box-Blur）"><a href="#盒状模糊或均值模糊（Box-Blur）" class="headerlink" title="盒状模糊或均值模糊（Box Blur）"></a>盒状模糊或均值模糊（Box Blur）</h2><p>最简单的模糊方式，取一个2*2的卷积核，四个数权重都为0.25，取一个像素的四角的像素叠加乘0.25</p>
<p><img src="https://img-blog.csdnimg.cn/b3aa70f8bcb943be9a634b4a5b87acf4.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_19,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<p>（-1，-1）（1，-1）（-1，1）（1，1）四个点位叠加除4；</p>
<p>当然卷积核的值可自己调</p>
<h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><h4 id="BoxBlur-cs"><a href="#BoxBlur-cs" class="headerlink" title="BoxBlur.cs"></a>BoxBlur.cs</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line">[<span class="meta">ExecuteInEditMode()</span>]</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">BoxBlur</span> : <span class="title">MonoBehaviour</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> Material material;</span><br><span class="line">    [<span class="meta">Range(0, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> _Iteration = <span class="number">4</span>;</span><br><span class="line">    [<span class="meta">Range(0, 15)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _BlurRadius = <span class="number">5.0f</span>;</span><br><span class="line">    [<span class="meta">Range(1, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _DownSample = <span class="number">2.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Start</span> ()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (material == <span class="literal">null</span> || SystemInfo.supportsImageEffects == <span class="literal">false</span></span><br><span class="line">            || material.shader == <span class="literal">null</span> || material.shader.isSupported == <span class="literal">false</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            enabled = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnRenderImage</span>(<span class="params">RenderTexture source, RenderTexture destination</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">int</span> width = (<span class="built_in">int</span>)(source.width / _DownSample);</span><br><span class="line">        <span class="built_in">int</span> height = (<span class="built_in">int</span>)(source.height / _DownSample);</span><br><span class="line">        RenderTexture RT1 = RenderTexture.GetTemporary(width,height);</span><br><span class="line">        RenderTexture RT2 = RenderTexture.GetTemporary(width, height);</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(source, RT1);</span><br><span class="line"></span><br><span class="line">        material.SetVector(<span class="string">&quot;_BlurOffset&quot;</span>, <span class="keyword">new</span> Vector4(_BlurRadius / source.width, _BlurRadius / source.height, <span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">0</span>);</span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(RT1, destination);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//release</span></span><br><span class="line">        RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">        RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="BoxBlur-shader"><a href="#BoxBlur-shader" class="headerlink" title="BoxBlur.shader"></a>BoxBlur.shader</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">Shader <span class="string">&quot;Hidden/BoxBlur&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">  CGINCLUDE</span><br><span class="line">  <span class="meta">#<span class="keyword">include</span> <span class="string">&quot;UnityCG.cginc&quot;</span></span></span><br><span class="line"></span><br><span class="line">  sampler2D _MainTex;</span><br><span class="line">  float4 _BlurOffset;</span><br><span class="line"></span><br><span class="line">  half4 <span class="title function_">frag_BoxFilter_4Tap</span><span class="params">(v2f_img i)</span> : SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    half4 d = _BlurOffset.xyxy * half4(<span class="number">-1</span>,<span class="number">-1</span>,<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">    half4 s = <span class="number">0</span>;</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.xy);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.zy);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.xw);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.zw);</span><br><span class="line">    s *= <span class="number">0.25</span>;</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  half4 <span class="title function_">frag_BoxFilter_9Tap</span><span class="params">(v2f_img i)</span> : SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">//half4 d = _BlurOffset.xyxy * half4(-1,1,0,0);</span></span><br><span class="line">    half4 d = _BlurOffset.xyxy * half4(<span class="number">-1</span>, <span class="number">-1</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    half4 s = <span class="number">0</span>;</span><br><span class="line">    s = tex2D(_MainTex, i.uv);</span><br><span class="line"></span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.xy);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.zy);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.xw);</span><br><span class="line">    s += tex2D(_MainTex, i.uv + d.zw);</span><br><span class="line"></span><br><span class="line">    s += tex2D(_MainTex, i.uv + half2(<span class="number">0.0</span>, d.w)); <span class="comment">// 0 1</span></span><br><span class="line">    s += tex2D(_MainTex, i.uv + half2(<span class="number">0.0</span>, d.y)); <span class="comment">// 0 -1</span></span><br><span class="line">    s += tex2D(_MainTex, i.uv + half2(d.z,<span class="number">0.0</span>)); <span class="comment">// 1 0</span></span><br><span class="line">    s += tex2D(_MainTex, i.uv + half2(d.x, <span class="number">0.0</span>)); <span class="comment">// -1 0</span></span><br><span class="line"></span><br><span class="line">    s = s/ <span class="number">9.0</span>;</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  ENDCG</span><br><span class="line"></span><br><span class="line">  Properties</span><br><span class="line">  &#123;</span><br><span class="line">    _MainTex (<span class="string">&quot;Texture&quot;</span>, <span class="number">2</span>D) = <span class="string">&quot;white&quot;</span> &#123;&#125;</span><br><span class="line">    _BlurOffset(<span class="string">&quot;BlurOffset&quot;</span>,Float) = <span class="number">1</span> </span><br><span class="line">  &#125;</span><br><span class="line">  SubShader</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// No culling or depth</span></span><br><span class="line">    Cull Off ZWrite Off ZTest Always</span><br><span class="line">    <span class="comment">//0pass 4tap</span></span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex vert_img</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment frag_BoxFilter_4Tap</span></span><br><span class="line">      ENDCG</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//1pass 9tap</span></span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex vert_img</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment frag_BoxFilter_9Tap</span></span><br><span class="line">      ENDCG</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="高斯模糊（Gaussian-Blur）"><a href="#高斯模糊（Gaussian-Blur）" class="headerlink" title="高斯模糊（Gaussian Blur）"></a>高斯模糊（Gaussian Blur）</h2><p>特定的权重，离像素点越远权重越小</p>
<p>下图为高斯函数的3维图示：</p>
<p><img src="https://pic4.zhimg.com/80/v2-d6af23aafa14caec06b3a1790b6d92e3_720w.jpg"></p>
<p>0.05 0.25 0.40 0.25 0.05权值</p>
<p>和盒状模糊差不多，效果会好一点</p>
<h3 id="代码-1"><a href="#代码-1" class="headerlink" title="代码"></a>代码</h3><h4 id="GaussianBlur-cs"><a href="#GaussianBlur-cs" class="headerlink" title="GaussianBlur.cs"></a>GaussianBlur.cs</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line">[<span class="meta">ExecuteInEditMode()</span>]</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">GaussianBlur</span> : <span class="title">MonoBehaviour</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> Material material;</span><br><span class="line">    [<span class="meta">Range(0, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> _Iteration = <span class="number">4</span>;</span><br><span class="line">    [<span class="meta">Range(0, 15)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _BlurRadius = <span class="number">5.0f</span>;</span><br><span class="line">    [<span class="meta">Range(1, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _DownSample = <span class="number">2.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Start</span> ()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (material == <span class="literal">null</span> || SystemInfo.supportsImageEffects == <span class="literal">false</span></span><br><span class="line">            || material.shader == <span class="literal">null</span> || material.shader.isSupported == <span class="literal">false</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            enabled = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnRenderImage</span>(<span class="params">RenderTexture source, RenderTexture destination</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">int</span> width = (<span class="built_in">int</span>)(source.width / _DownSample);</span><br><span class="line">        <span class="built_in">int</span> height = (<span class="built_in">int</span>)(source.height / _DownSample);</span><br><span class="line">        RenderTexture RT1 = RenderTexture.GetTemporary(width,height);</span><br><span class="line">        RenderTexture RT2 = RenderTexture.GetTemporary(width, height);</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(source, RT1);</span><br><span class="line"></span><br><span class="line">        material.SetVector(<span class="string">&quot;_BlurOffset&quot;</span>, <span class="keyword">new</span> Vector4(_BlurRadius / source.width, _BlurRadius / source.height, <span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">0</span>); <span class="comment">//水平方向</span></span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">1</span>); <span class="comment">//垂直方向</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(RT1, destination);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//release</span></span><br><span class="line">        RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">        RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="GaussianBlur-shader"><a href="#GaussianBlur-shader" class="headerlink" title="GaussianBlur.shader"></a>GaussianBlur.shader</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">Shader <span class="string">&quot;Hidden/GaussianBlur&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">  CGINCLUDE</span><br><span class="line">  <span class="meta">#<span class="keyword">include</span> <span class="string">&quot;UnityCG.cginc&quot;</span></span></span><br><span class="line"></span><br><span class="line">  sampler2D _MainTex;</span><br><span class="line">  float4 _BlurOffset;</span><br><span class="line"></span><br><span class="line">  half4 <span class="title function_">frag_HorizontalBlur</span><span class="params">(v2f_img i)</span> : SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    half2 uv1 = i.uv + _BlurOffset.xy * half2(<span class="number">1</span>, <span class="number">0</span>) * <span class="number">-2.0</span>;</span><br><span class="line">    half2 uv2 = i.uv + _BlurOffset.xy * half2(<span class="number">1</span>, <span class="number">0</span>) * <span class="number">-1.0</span>;</span><br><span class="line">    half2 uv3 = i.uv;</span><br><span class="line">    half2 uv4 = i.uv + _BlurOffset.xy * half2(<span class="number">1</span>, <span class="number">0</span>) * <span class="number">1.0</span>;</span><br><span class="line">    half2 uv5 = i.uv + _BlurOffset.xy * half2(<span class="number">1</span>, <span class="number">0</span>) * <span class="number">2.0</span>;</span><br><span class="line"></span><br><span class="line">    half4 s = <span class="number">0</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv1) * <span class="number">0.05</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv2) * <span class="number">0.25</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv3) * <span class="number">0.40</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv4) * <span class="number">0.25</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv5) * <span class="number">0.05</span>;</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  half4 <span class="title function_">frag_VerticalBlur</span><span class="params">(v2f_img i)</span> : SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    half2 uv1 = i.uv + _BlurOffset.xy * half2(<span class="number">0</span>, <span class="number">1</span>) * <span class="number">-2.0</span>;</span><br><span class="line">    half2 uv2 = i.uv + _BlurOffset.xy * half2(<span class="number">0</span>, <span class="number">1</span>) * <span class="number">-1.0</span>;</span><br><span class="line">    half2 uv3 = i.uv;</span><br><span class="line">    half2 uv4 = i.uv + _BlurOffset.xy * half2(<span class="number">0</span>, <span class="number">1</span>) * <span class="number">1.0</span>;</span><br><span class="line">    half2 uv5 = i.uv + _BlurOffset.xy * half2(<span class="number">0</span>, <span class="number">1</span>) * <span class="number">2.0</span>;</span><br><span class="line"></span><br><span class="line">    half4 s = <span class="number">0</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv1) * <span class="number">0.05</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv2) * <span class="number">0.25</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv3) * <span class="number">0.40</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv4) * <span class="number">0.25</span>;</span><br><span class="line">    s += tex2D(_MainTex, uv5) * <span class="number">0.05</span>;</span><br><span class="line">    <span class="keyword">return</span> s;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  ENDCG</span><br><span class="line"></span><br><span class="line">  Properties</span><br><span class="line">  &#123;</span><br><span class="line">    _MainTex (<span class="string">&quot;Texture&quot;</span>, <span class="number">2</span>D) = <span class="string">&quot;white&quot;</span> &#123;&#125;</span><br><span class="line">    _BlurOffset(<span class="string">&quot;BlurOffset&quot;</span>,Float) = <span class="number">1</span> </span><br><span class="line">  &#125;</span><br><span class="line">  SubShader</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// No culling or depth</span></span><br><span class="line">    Cull Off ZWrite Off ZTest Always</span><br><span class="line">    <span class="comment">//0</span></span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex vert_img</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment frag_HorizontalBlur</span></span><br><span class="line">      ENDCG</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//1</span></span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex vert_img</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment frag_VerticalBlur</span></span><br><span class="line">      ENDCG</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="双重模糊"><a href="#双重模糊" class="headerlink" title="双重模糊"></a>双重模糊</h2><p>双重的核心思想是先降采样，然后升采样。</p>
<p>相较于Kawase Blur在两个大小相等的纹理之间进行乒乓blit的的思路，Dual Kawase Blur的核心思路在于blit过程中进行降采样和升采样,即对RT进行了降采样以及升采样。如下图所示：</p>
<p><img src="https://pic4.zhimg.com/80/v2-4226f0fd81e3f2ed61b48995e269328f_720w.jpg"></p>
<h3 id="双重-Kawase-模糊（Dual-Kawase-Blur）"><a href="#双重-Kawase-模糊（Dual-Kawase-Blur）" class="headerlink" title="双重 Kawase 模糊（Dual Kawase Blur）"></a>双重 Kawase 模糊（Dual Kawase Blur）</h3><p>双重 Kawase 模糊和双重盒状模糊模糊是最常用的，前者常用于移动端，后者都行，两个的性能和质量都优于之前两种算法</p>
<h4 id="Kawase模糊"><a href="#Kawase模糊" class="headerlink" title="Kawase模糊"></a>Kawase模糊</h4><p>Kawase Blur的思路是对距离当前像素越来越远的地方对四个角进行采样</p>
<p><img src="https://pic4.zhimg.com/80/v2-5ef03cc98ed2ed9a22dee30ce2915e7f_720w.jpg"></p>
<p>所以中间的权重为4&#x2F;8，其余四角为1&#x2F;8；</p>
<p>单独的Kawase算法性能太差了，淘汰。</p>
<h4 id="Dual-Kawase-Blur"><a href="#Dual-Kawase-Blur" class="headerlink" title="Dual Kawase Blur"></a>Dual Kawase Blur</h4><p>Dual Kawase Blur，简称Dual Blur，是SIGGRAPH 2015上ARM团队提出的一种衍生自Kawase Blur的模糊算法。其由两种不同的Blur Kernel构成，如下图所示。<img src="https://pic1.zhimg.com/80/v2-1ae54eb0e154d542ff6acdae06232bdc_720w.jpg"></p>
<p><img src="https://img-blog.csdnimg.cn/e92e5664bde1419f830f6a0e5dc9b91a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAUEMgRXJyb3I=,size_16,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<p>这个贴图怕是只有我看的懂了，图一的1图代表红色的框框，第二个代表整个框框</p>
<h4 id="代码-2"><a href="#代码-2" class="headerlink" title="代码"></a>代码</h4><h4 id="DualKawaseBlur-cs"><a href="#DualKawaseBlur-cs" class="headerlink" title="DualKawaseBlur.cs"></a>DualKawaseBlur.cs</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"></span><br><span class="line">[<span class="meta">RequireComponent(typeof(Camera))</span>]</span><br><span class="line">[<span class="meta">ExecuteInEditMode</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">DualKawaseBlur</span> : <span class="title">MonoBehaviour</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Material material;</span><br><span class="line">    [<span class="meta">Range(0,15)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _BlurRadius = <span class="number">5.0f</span>;</span><br><span class="line">    [<span class="meta">Range(0, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> _Iteration = <span class="number">4</span>;</span><br><span class="line">    [<span class="meta">Range(1, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _DownSample = <span class="number">2.0f</span>;</span><br><span class="line"></span><br><span class="line">    List&lt;RenderTexture&gt; _tempRTList = <span class="keyword">new</span> List&lt;RenderTexture&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Start</span>()</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (!SystemInfo.supportsImageEffects || <span class="literal">null</span> == material ||</span><br><span class="line">           <span class="literal">null</span> == material.shader || !material.shader.isSupported)</span><br><span class="line">        &#123;</span><br><span class="line">            enabled = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnRenderImage</span>(<span class="params">RenderTexture source, RenderTexture destination</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">int</span> RTWidth = (<span class="built_in">int</span>)(source.width / _DownSample);</span><br><span class="line">        <span class="built_in">int</span> RTHeight = (<span class="built_in">int</span>)(source.height / _DownSample);</span><br><span class="line">        RenderTexture RT1 = RenderTexture.GetTemporary(RTWidth, RTHeight, <span class="number">0</span>);</span><br><span class="line">        RenderTexture RT2 = <span class="literal">null</span>;</span><br><span class="line">        material.SetFloat(<span class="string">&quot;_Offset&quot;</span>, _BlurRadius);</span><br><span class="line">        Graphics.Blit(source, RT1, material, <span class="number">0</span>);</span><br><span class="line"><span class="comment">//降采样</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">            RTWidth = RTWidth / <span class="number">2</span>;</span><br><span class="line">            RTHeight = RTHeight / <span class="number">2</span>;</span><br><span class="line">            RT2 = RenderTexture.GetTemporary(RTWidth, RTHeight);</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">            RTWidth = RTWidth / <span class="number">2</span>;</span><br><span class="line">            RTHeight = RTHeight / <span class="number">2</span>;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">            RT1 = RenderTexture.GetTemporary(RTWidth, RTHeight);</span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//升采样</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">            RTWidth = RTWidth * <span class="number">2</span>;</span><br><span class="line">            RTHeight = RTHeight * <span class="number">2</span>;</span><br><span class="line">            RT2 = RenderTexture.GetTemporary(RTWidth, RTHeight);</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">            RTWidth = RTWidth * <span class="number">2</span>;</span><br><span class="line">            RTHeight = RTHeight * <span class="number">2</span>;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">            RT1 = RenderTexture.GetTemporary(RTWidth, RTHeight);</span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(RT1, destination, material, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// release</span></span><br><span class="line">        RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">        RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="DualKawaseBlur-shader"><a href="#DualKawaseBlur-shader" class="headerlink" title="DualKawaseBlur.shader"></a>DualKawaseBlur.shader</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><span class="line">Shader <span class="string">&quot;Hidden/DualKawaseBlur&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">  CGINCLUDE</span><br><span class="line">  <span class="meta">#<span class="keyword">include</span> <span class="string">&quot;UnityCG.cginc&quot;</span></span></span><br><span class="line">  uniform sampler2D _MainTex;</span><br><span class="line">  uniform float4 _MainTex_TexelSize;</span><br><span class="line">  uniform half _Offset;</span><br><span class="line">  </span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">v2f_DownSample</span></span></span><br><span class="line"><span class="class">  &#123;</span></span><br><span class="line">    float4 pos: SV_POSITION;</span><br><span class="line">    float2 uv: TEXCOORD1;</span><br><span class="line">    float4 uv01: TEXCOORD2;</span><br><span class="line">    float4 uv23: TEXCOORD3;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">v2f_UpSample</span></span></span><br><span class="line"><span class="class">  &#123;</span></span><br><span class="line">    float4 pos: SV_POSITION;</span><br><span class="line">    float4 uv01: TEXCOORD1;</span><br><span class="line">    float4 uv23: TEXCOORD2;</span><br><span class="line">    float4 uv45: TEXCOORD3;</span><br><span class="line">    float4 uv67: TEXCOORD4;</span><br><span class="line">  &#125;;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  v2f_DownSample <span class="title function_">Vert_DownSample</span><span class="params">(appdata_img v)</span></span><br><span class="line">  &#123;</span><br><span class="line">    v2f_DownSample o;</span><br><span class="line">    o.pos = UnityObjectToClipPos(v.vertex);</span><br><span class="line">    </span><br><span class="line">    _MainTex_TexelSize = <span class="number">0.5</span> * _MainTex_TexelSize;</span><br><span class="line">    float2 uv = v.texcoord;</span><br><span class="line">    o.uv = uv;</span><br><span class="line">    o.uv01.xy = uv - _MainTex_TexelSize * float2(<span class="number">1</span> + _Offset, <span class="number">1</span> + _Offset);<span class="comment">//top right</span></span><br><span class="line">    o.uv01.zw = uv + _MainTex_TexelSize * float2(<span class="number">1</span> + _Offset, <span class="number">1</span> + _Offset);<span class="comment">//bottom left</span></span><br><span class="line">    o.uv23.xy = uv - float2(_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * float2(<span class="number">1</span> + _Offset, <span class="number">1</span> + _Offset);<span class="comment">//top left</span></span><br><span class="line">    o.uv23.zw = uv + float2(_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * float2(<span class="number">1</span> + _Offset, <span class="number">1</span> + _Offset);<span class="comment">//bottom right</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> o;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  half4 <span class="title function_">Frag_DownSample</span><span class="params">(v2f_DownSample i)</span>: SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    half4 sum = tex2D(_MainTex, i.uv) * <span class="number">4</span>;</span><br><span class="line">    sum += tex2D(_MainTex, i.uv01.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv01.zw);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv23.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv23.zw);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> sum * <span class="number">0.125</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  v2f_UpSample <span class="title function_">Vert_UpSample</span><span class="params">(appdata_img v)</span></span><br><span class="line">  &#123;</span><br><span class="line">    v2f_UpSample o;</span><br><span class="line">    o.pos = UnityObjectToClipPos(v.vertex);</span><br><span class="line">  </span><br><span class="line">    float2 uv = v.texcoord;</span><br><span class="line">    </span><br><span class="line">    _MainTex_TexelSize = <span class="number">0.5</span> * _MainTex_TexelSize;</span><br><span class="line">    _Offset = float2(<span class="number">1</span> + _Offset, <span class="number">1</span> + _Offset);</span><br><span class="line">    </span><br><span class="line">    o.uv01.xy = uv + float2(-_MainTex_TexelSize.x * <span class="number">2</span>, <span class="number">0</span>) * _Offset;<span class="comment">//-2,0</span></span><br><span class="line">    o.uv01.zw = uv + float2(-_MainTex_TexelSize.x, _MainTex_TexelSize.y) * _Offset;<span class="comment">//-1 1</span></span><br><span class="line">    o.uv23.xy = uv + float2(<span class="number">0</span>, _MainTex_TexelSize.y * <span class="number">2</span>) * _Offset;<span class="comment">//0 2</span></span><br><span class="line">    o.uv23.zw = uv + _MainTex_TexelSize * _Offset;<span class="comment">//1 1</span></span><br><span class="line">    o.uv45.xy = uv + float2(_MainTex_TexelSize.x * <span class="number">2</span>, <span class="number">0</span>) * _Offset;<span class="comment">//2 0</span></span><br><span class="line">    o.uv45.zw = uv + float2(_MainTex_TexelSize.x, -_MainTex_TexelSize.y) * _Offset;<span class="comment">//1 -1</span></span><br><span class="line">    o.uv67.xy = uv + float2(<span class="number">0</span>, -_MainTex_TexelSize.y * <span class="number">2</span>) * _Offset;<span class="comment">//0 -2</span></span><br><span class="line">    o.uv67.zw = uv - _MainTex_TexelSize * _Offset;<span class="comment">// -1 -1</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> o;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  half4 <span class="title function_">Frag_UpSample</span><span class="params">(v2f_UpSample i)</span>: SV_Target</span><br><span class="line">  &#123;</span><br><span class="line">    half4 sum = <span class="number">0</span>;</span><br><span class="line">    sum += tex2D(_MainTex, i.uv01.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv01.zw) * <span class="number">2</span>;</span><br><span class="line">    sum += tex2D(_MainTex, i.uv23.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv23.zw) * <span class="number">2</span>;</span><br><span class="line">    sum += tex2D(_MainTex, i.uv45.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv45.zw) * <span class="number">2</span>;</span><br><span class="line">    sum += tex2D(_MainTex, i.uv67.xy);</span><br><span class="line">    sum += tex2D(_MainTex, i.uv67.zw) * <span class="number">2</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> sum * <span class="number">0.0833</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  ENDCG</span><br><span class="line">  Properties</span><br><span class="line">    &#123;</span><br><span class="line">        _MainTex(<span class="string">&quot;&quot;</span>, <span class="number">2</span>D) = <span class="string">&quot;white&quot;</span> &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">  SubShader</span><br><span class="line">  &#123;</span><br><span class="line">    Cull Off ZWrite Off ZTest Always</span><br><span class="line">    </span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex Vert_DownSample</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment Frag_DownSample	</span></span><br><span class="line">      ENDCG		</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    Pass</span><br><span class="line">    &#123;</span><br><span class="line">      CGPROGRAM	</span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> vertex Vert_UpSample</span></span><br><span class="line">      <span class="meta">#<span class="keyword">pragma</span> fragment Frag_UpSample</span></span><br><span class="line">      ENDCG</span><br><span class="line">      </span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="双重盒状模糊（Dual-Box-Blur）"><a href="#双重盒状模糊（Dual-Box-Blur）" class="headerlink" title="双重盒状模糊（Dual Box Blur）"></a>双重盒状模糊（Dual Box Blur）</h4><p>对原来的Box进行双重采样就行，最常用的算法。</p>
<h4 id="代码-3"><a href="#代码-3" class="headerlink" title="代码"></a>代码</h4><h4 id="DualBoxBlur-cs"><a href="#DualBoxBlur-cs" class="headerlink" title="DualBoxBlur.cs"></a>DualBoxBlur.cs</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.Collections.Generic;</span><br><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"></span><br><span class="line">[<span class="meta">ExecuteInEditMode()</span>]</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">DualBoxBlur</span> : <span class="title">MonoBehaviour</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> Material material;</span><br><span class="line">    [<span class="meta">Range(0, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">int</span> _Iteration = <span class="number">4</span>;</span><br><span class="line">    [<span class="meta">Range(0, 15)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _BlurRadius = <span class="number">5.0f</span>;</span><br><span class="line">    [<span class="meta">Range(1, 10)</span>]</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">float</span> _DownSample = <span class="number">2.0f</span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">Start</span> ()</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (material == <span class="literal">null</span> || SystemInfo.supportsImageEffects == <span class="literal">false</span></span><br><span class="line">            || material.shader == <span class="literal">null</span> || material.shader.isSupported == <span class="literal">false</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            enabled = <span class="literal">false</span>;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">OnRenderImage</span>(<span class="params">RenderTexture source, RenderTexture destination</span>)</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">int</span> width = (<span class="built_in">int</span>)(source.width / _DownSample);</span><br><span class="line">        <span class="built_in">int</span> height = (<span class="built_in">int</span>)(source.height / _DownSample);</span><br><span class="line">        RenderTexture RT1 = RenderTexture.GetTemporary(width,height);</span><br><span class="line">        RenderTexture RT2 = RenderTexture.GetTemporary(width, height);</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(source, RT1);</span><br><span class="line"></span><br><span class="line">        material.SetVector(<span class="string">&quot;_BlurOffset&quot;</span>, <span class="keyword">new</span> Vector4(_BlurRadius / source.width, _BlurRadius / source.height, <span class="number">0</span>,<span class="number">0</span>));</span><br><span class="line">        <span class="comment">//降采样</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">            width = width / <span class="number">2</span>;</span><br><span class="line">            height = height / <span class="number">2</span>;</span><br><span class="line">            RT2 = RenderTexture.GetTemporary(width, height);</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">            RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">            width = width / <span class="number">2</span>;</span><br><span class="line">            height = height / <span class="number">2</span>;</span><br><span class="line">            RT1 = RenderTexture.GetTemporary(width, height);</span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//升采样</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">int</span> i = <span class="number">0</span>; i &lt; _Iteration; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">            width = width * <span class="number">2</span>;</span><br><span class="line">            height = height * <span class="number">2</span>;</span><br><span class="line">            RT2 = RenderTexture.GetTemporary(width, height);</span><br><span class="line">            Graphics.Blit(RT1, RT2, material, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">            RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">            width = width * <span class="number">2</span>;</span><br><span class="line">            height = height * <span class="number">2</span>;</span><br><span class="line">            RT1 = RenderTexture.GetTemporary(width, height);</span><br><span class="line">            Graphics.Blit(RT2, RT1, material, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Graphics.Blit(RT1, destination);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//release</span></span><br><span class="line">        RenderTexture.ReleaseTemporary(RT1);</span><br><span class="line">        RenderTexture.ReleaseTemporary(RT2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>就之前的BoxBlur.shader。</p>
<h1 id="其余算法"><a href="#其余算法" class="headerlink" title="其余算法"></a>其余算法</h1><p>可以参考毛星云大佬的其他算法。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="/music/jucilang.mp3">
            </audio>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='99f95a936c6fcdb1bd67'
        data-cs='c2d1bba3d6b7f9ade4f3de09a63925bd837c25ea'
        data-r='mrchenlearnspace.github.io'
        data-o='MrChenLearnSpace'
        data-a='MrChenLearnSpace'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%A8%A1%E7%B3%8A%E5%A4%84%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">模糊处理</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%AF%BE%E7%A8%8B%E7%9A%844%E7%A7%8D%E5%B8%B8%E7%94%A8%E7%9A%84%E6%A8%A1%E7%B3%8A%E7%AE%97%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">课程的4种常用的模糊算法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%92%E7%8A%B6%E6%A8%A1%E7%B3%8A%E6%88%96%E5%9D%87%E5%80%BC%E6%A8%A1%E7%B3%8A%EF%BC%88Box-Blur%EF%BC%89"><span class="toc-number">2.1.</span> <span class="toc-text">盒状模糊或均值模糊（Box Blur）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">2.1.1.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#BoxBlur-cs"><span class="toc-number">2.1.1.1.</span> <span class="toc-text">BoxBlur.cs</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#BoxBlur-shader"><span class="toc-number">2.1.1.2.</span> <span class="toc-text">BoxBlur.shader</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AB%98%E6%96%AF%E6%A8%A1%E7%B3%8A%EF%BC%88Gaussian-Blur%EF%BC%89"><span class="toc-number">2.2.</span> <span class="toc-text">高斯模糊（Gaussian Blur）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-1"><span class="toc-number">2.2.1.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#GaussianBlur-cs"><span class="toc-number">2.2.1.1.</span> <span class="toc-text">GaussianBlur.cs</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#GaussianBlur-shader"><span class="toc-number">2.2.1.2.</span> <span class="toc-text">GaussianBlur.shader</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8C%E9%87%8D%E6%A8%A1%E7%B3%8A"><span class="toc-number">2.3.</span> <span class="toc-text">双重模糊</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%8C%E9%87%8D-Kawase-%E6%A8%A1%E7%B3%8A%EF%BC%88Dual-Kawase-Blur%EF%BC%89"><span class="toc-number">2.3.1.</span> <span class="toc-text">双重 Kawase 模糊（Dual Kawase Blur）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Kawase%E6%A8%A1%E7%B3%8A"><span class="toc-number">2.3.1.1.</span> <span class="toc-text">Kawase模糊</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Dual-Kawase-Blur"><span class="toc-number">2.3.1.2.</span> <span class="toc-text">Dual Kawase Blur</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-2"><span class="toc-number">2.3.1.3.</span> <span class="toc-text">代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DualKawaseBlur-cs"><span class="toc-number">2.3.1.4.</span> <span class="toc-text">DualKawaseBlur.cs</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DualKawaseBlur-shader"><span class="toc-number">2.3.1.5.</span> <span class="toc-text">DualKawaseBlur.shader</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8C%E9%87%8D%E7%9B%92%E7%8A%B6%E6%A8%A1%E7%B3%8A%EF%BC%88Dual-Box-Blur%EF%BC%89"><span class="toc-number">2.3.1.6.</span> <span class="toc-text">双重盒状模糊（Dual Box Blur）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-3"><span class="toc-number">2.3.1.7.</span> <span class="toc-text">代码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DualBoxBlur-cs"><span class="toc-number">2.3.1.8.</span> <span class="toc-text">DualBoxBlur.cs</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%B6%E4%BD%99%E7%AE%97%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">其余算法</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/blog/js/plugin.js"></script>
<script src="/blog/js/typed.js"></script>
<script src="/blog/js/diaspora.js"></script>


<link rel="stylesheet" href="/blog/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/blog/photoswipe/default-skin/default-skin.css">


<script src="/blog/photoswipe/photoswipe.min.js"></script>
<script src="/blog/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>



<!-- Google Analytics -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YMJ8CBH8F7"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-YMJ8CBH8F7');
</script>
<!-- End Google Analytics -->


</html>
